package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.util.UserContext;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @ResponseBody
    @RequestMapping("/login")
    public JsonResult login(String username , String password){
        try {
            Subject subject = SecurityUtils.getSubject();
            Md5Hash md5Hash = new Md5Hash(password, username, 1);
            UsernamePasswordToken token = new UsernamePasswordToken(username, md5Hash.toString());
            subject.login(token);
            return new JsonResult();
        } catch(UnknownAccountException e) {
            e.printStackTrace();
            return new JsonResult(false,"用户名不存在");
        }catch (IncorrectCredentialsException e){
            return new JsonResult(false,"密码错误");
        }catch (DisabledAccountException e){
            return new JsonResult(false,e.getMessage());
        }
    }
}
